package com.iot08.api.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iot08.common.constant.Const;
import com.iot08.common.utils.StringUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @version 1.0
 * @Description:
 * @time: 2021/1/21 14:41
 * @author: Rock
 */
public class TCPUtil {


    /**
     * 比较cells是否相同
     *
     * @param cells
     * @param newCells
     * @time: 2021/1/21 15:35
     * @author: Rock
     * @version 1.0
     */
    public static int cellsCompare(JSONArray cells, JSONArray newCells) {
        if (cells != null && newCells != null && cells.size() == newCells.size()) {
            for (int i = 0; i < cells.size(); i++) {
                JSONObject item = JSON.parseObject(cells.getJSONObject(i).toJSONString());
                JSONObject newItem = JSON.parseObject(newCells.getJSONObject(i).toJSONString());
                if (item.size() != newItem.size()) {
                    return 0;
                }
                if (item.getInteger("rxlev") != null && newItem.getInteger("rxlev") != null) {
                    if (Math.abs(item.getInteger("rxlev") - newItem.getInteger("rxlev")) > 30) {
                        return 0;
                    }
                }
                item.remove("rxlev");
                newItem.remove("rxlev");
                item.remove("timestamp");
                newItem.remove("timestamp");
                for (String key : newItem.keySet()) {
                    if (!newItem.get(key).equals(item.get(key))) {
                        return 0;
                    }
                }
            }
            return 1;
        }
        return 0;
    }

    /**
     * @param telecomcell
     * @param newTelecomcell
     * @time: 2021/1/21 15:53
     * @author: Rock
     * @version 1.0
     */
    public static int telecomcellCompare(JSONObject telecomcell, JSONObject newTelecomcell) {
        if (telecomcell != null && newTelecomcell != null && telecomcell.size() == newTelecomcell.size()) {
            JSONObject telecomcellCopy = JSON.parseObject(telecomcell.toJSONString());
            JSONObject newTelecomcellCopy = JSON.parseObject(newTelecomcell.toJSONString());
            if (telecomcellCopy.getInteger("rxlev") != null && newTelecomcellCopy.getInteger("rxlev") != null) {
                if (Math.abs(telecomcellCopy.getInteger("rxlev") - newTelecomcellCopy.getInteger("rxlev")) > 30) {
                    return 0;
                }
            }
            telecomcellCopy.remove("rxlev");
            newTelecomcellCopy.remove("rxlev");
//            telecomcellCopy.remove("timestamp");
//            newTelecomcellCopy.remove("timestamp");
            for (String key : newTelecomcellCopy.keySet()) {
                if (!newTelecomcellCopy.get(key).equals(telecomcellCopy.get(key))) {
                    return 0;
                }
            }
            return 1;
        }
        return 0;
    }

    /**
     * 比较 Wifi 是否相似  0: 不相似  1: 相似
     * @param wifis
     * @param newWifis
     * @time: 2021/1/21 17:01
     * @author: Rock
     * @version 1.0
     */
    public static int wifwsCompare(JSONArray wifis, JSONArray newWifis) {
        if (wifis != null && newWifis != null && wifis.size() == newWifis.size()) {
            JSONObject temp = null;
            JSONObject newTemp = null;
            for (int i = 0; i < wifis.size(); i++) {
                if (Const.INT_1.equals(wifis.getJSONObject(i).get("connected"))) {
                    temp = wifis.getJSONObject(i);
                }
                if (Const.INT_1.equals(newWifis.getJSONObject(i).get("connected"))) {
                    newTemp = newWifis.getJSONObject(i);
                }
            }
            if (temp != null && newTemp != null) {
                if (StringUtil.isNotBlank(temp.get("mac")) && StringUtil.isNotBlank(newTemp.get("mac"))
                        && !temp.getString("mac").toUpperCase().equals(temp.getString("mac").toUpperCase())) {
                    return 0;
                }
            } else if (temp == null && newTemp == null) {
                int diffNum = 0;
                for (int i = 0; i < wifis.size(); i++) {
                    String mac = wifis.getJSONObject(i).getString("mac");
                    boolean isFind = false;
                    if (StringUtil.isNotBlank(mac)) {
                        for (int j = 0; j < newWifis.size(); j++) {
                            if (newWifis.getJSONObject(j).containsKey("mac")
                                    && mac.toLowerCase().equals(newWifis.getJSONObject(j).getString("mac").toLowerCase())) {
                                isFind = true;
                            }
                        }
                    }
                    if (!isFind) {
                        diffNum += 1;
                    }
                }
                //不同的个数超过wifi上传个数的 1/5 那就进行重新定位
                if (diffNum * 1.0 / wifis.size() * 1.0 > 1.0 / 5.0) {
                    return 0;
                }
            } else {
                //WIFI 从有连接到没连接的变化 或者 从无到有的变化
                return 0;
            }
            return 1;
        }
        return 0;
    }

    /**
     * @param value
     * @time: 2021/3/3 18:41
     * @author: Rock
     * @version 1.0
     */
    public static Map<String, Object> jsonChangeMap(JSONObject value) {
        if (value != null) {
            Map<String, Object> result = new HashMap<>();
            Iterator it = value.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
                result.put(entry.getKey(), entry.getValue());
            }
            return result;
        }
        return null;
    }

    /**
     * @param rxlev
     * @time: 2021/3/4 9:56
     * @author: Rock
     * @version 1.0
     */
    public static int rxlevHandle(Object rxlev) {
        int rx = -48;
        if (StringUtil.isNumeric(rxlev)) {
            rx = new Double(rxlev.toString()).intValue();
            if (rx > 0) {
                rx = 2 * rx - 113;
            }
        }
        return rx;
    }


}
